/**
 * @author EasonChan
 *
 * Nov 15, 2010
 */
package com.musicgeek.biz.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.musicgeek.dao.User;
import com.musicgeek.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Register extends ActionSupport implements SessionAware,
		ModelDriven<User> {
	private static final long serialVersionUID = -6577615713214551671L;

	public String execute() {

		String name = user.getName();
		String password = user.getPassword();
		String email = user.getEmail();
		int sex = user.getSex().equals("男") ? 0 : 1;
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		int level = User.INIT_LEVEL;

		UserDao.addUser(name, password, email, sex, date, level);
		session.put(User.USER, getModel().getName());
		return SUCCESS;
	}

	@Override
	public void validate() {
		// String name = user.getName().toLowerCase();
		// String emial = user.getEmail();
		// String password = user.getPassword();
		// final String userNameRegex = "^[a-z0-9_-]{3,16}$";
		// final String pwdRegex = "^[a-z0-9_-]{6,18}$";
		// final String emailRegex =
		// "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";
		// if (!name.matches(userNameRegex))
		// addFieldError("user.name", "�û��������3-16���ַ�֮��");
		// if (!password.matches(pwdRegex))
		// addFieldError("user.password",
		// "���볤��Ϊ6-18���ַ�ֻ������ĸ���ֻ����»���");
		// if (!password.equalsIgnoreCase(getPwdConfirm()))
		// addFieldError("pwdConfirm", "������������벻ͬ");
		// if (!emial.matches(emailRegex))
		// addFieldError("user.email", "�ʼ���ַ��ʽ����");
		// if (UserDao.isUserExist(name)) {
		// addFieldError("user.name", "�û��Ѵ��ڣ������������û���");
		// }
	}

	private User user = new User();
	private String pwdConfirm;
	private Map<String, Object> session;

	@Override
	public User getModel() {
		return user;
	}

	public String getPwdConfirm() {
		return pwdConfirm;
	}

	public void setPwdConfirm(String pwdConfirm) {
		this.pwdConfirm = pwdConfirm;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
}
